using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyHocPhi.Entity;
using QuanLyHocPhi.DAL;
using QuanLyHocPhi.Util;
using System.Windows.Forms;
using UI.ObjectBindingSource;
namespace QuanLyHocPhi
{
    public partial class frmPhanlop : DevComponents.DotNetBar.Metro.MetroForm
    {
        
        DOI_TUONG_DAO dtDao = new DOI_TUONG_DAO();
        IList<DOI_TUONG> dtuong = null;
        LOP_HOC_DAO lhDao = new LOP_HOC_DAO();
        HOA_DON_DAO hdDao = new HOA_DON_DAO();
        IList<HOA_DON> dshoadon1 = null;
        IList<HOA_DON> dshoadon2 = null;
        IList<LOP_HOC> lhoc = null;
        HOA_DON hoaDonHienTai;
        LOP_HOC lophoc = new LOP_HOC();
        public frmPhanlop()
        {
            InitializeComponent();
        }

        private void labelX2_Click(object sender, EventArgs e)
        {

        }

        private void frmPhanlop_Load(object sender, EventArgs e)
        {
            //hien thi dan sach loai lop hoc
            dtuong = dtDao.GetObjectsByLOP("class_type");
            foreach (DOI_TUONG dt in dtuong)
            {
                cbbLoailop.Items.Add(dt);
                cbbLoailop2.Items.Add(dt);
            }
            //hien thi sinh vien chua co lop
            dshoadon1 = hdDao.GetActivatedInvoicesWithoutClass("%");
            dataGridViewX1.AutoGenerateColumns = false;
            


            dshoadon2 = hdDao.GetActivatedInvoicesHadClass("%",0);
            dataGridViewX2.AutoGenerateColumns = false;
           // NestedBindingSource<HOA_DON> list = new NestedBindingSource<HOA_DON>();
           // foreach (HOA_DON hd in dshoadon)
           // {
           //     list.Add(hd);
           // }
           // ObjectBindingSource source = new ObjectBindingSource();
            //BindingList<HOA_DON> list = new BindingList<HOA_DON>();
           // foreach (HOA_DON hd in dshoadon) { list.Add(hd); }
           // source.Add(
            objectBindingSource1.DataSource  = dshoadon1;
            dataGridViewX1.DataSource = objectBindingSource1;
            dataGridViewX1.AllowUserToAddRows = false;
            dataGridViewX1.AllowUserToDeleteRows = false;

            objectBindingSource2.DataSource = dshoadon2;
            dataGridViewX2.DataSource = objectBindingSource2;
            dataGridViewX2.AllowUserToAddRows = false;
            dataGridViewX2.AllowUserToDeleteRows = false;
            
            
           // hien thi danh sach lop hoc
            lhoc = lhDao.GetCurrentLOPHOC();
            foreach (LOP_HOC lh in lhoc)
            {
                cbbThemlop.Items.Add(lh);
            }

           // frmPhanlop_Load(sender, e);
            //int maLop = -1;
            //if (cbbThemlop.SelectedItem != null)
           // {
           //     maLop = ((LOP_HOC)cbbThemlop.SelectedItem).MA_LOP_HOC;
           // }
           // dshoadon = hdDao.GetActivatedInvoicesHadClass(txtTensinhvien.Text, maLop);
            //dataGridViewX1.AutoGenerateColumns = false;
            //dataGridViewX1.DataSource = dshoadon;

        }


        private void dataGridViewX1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           // DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
            ///CheckBox chk = new CheckBox();
            //CheckboxColumn.Width = 20;
          //  dataGridViewX1.Columns.Add(CheckboxColumn);
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            int err = 0;
            SINHVIEN_LOPHOC_DAO svlhDAO = new SINHVIEN_LOPHOC_DAO();
            for (int i = dataGridViewX1.Rows.Count - 1;i>=0 ; i--)
            {
                DataGridViewRow row = dataGridViewX1.Rows[i];
                DataGridViewCheckBoxCell chk = row.Cells[0] as DataGridViewCheckBoxCell;
               
                if (chk.Value != null && chk.Value.ToString() == "1")
                {

                    int maSV = KieuDuLieu.ChuyenSangsonguyen(row.Cells[4].Value.ToString(), out err);
                    if (cbbThemlop.SelectedItem != null)
                    {
                        int maLop = ((LOP_HOC)cbbThemlop.SelectedItem).MA_LOP_HOC;

                        SINH_VIEN sv = new SINH_VIEN() { MA_SV = maSV };
                        LOP_HOC lop = new LOP_HOC() { MA_LOP_HOC = maLop };
                        svlhDAO.Insert(new SINHVIEN_LOPHOC() { SINH_VIEN = sv, MA_LOP_HOC = lop });
                        //  row.ReadOnly = true;
                        //  row.Frozen = true;
                        hoaDonHienTai.MA_LOP_HOC.MA_LOP_HOC=maLop;
                        //hoaDonHienTai.MA_HOA_DON;
                        hdDao.Reupdate(hoaDonHienTai);
                        dataGridViewX1.Rows.RemoveAt(i);
                    }
                }
            }
                    //thong bao thanh cong

        }

        private void tabControlPanel1_Click(object sender, EventArgs e)
        {

        }

        private void tabControl1_Click(object sender, EventArgs e)
        {

        }

        private void cbbThemlop_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void buttonX3_Click(object sender, EventArgs e)
        {

        }

        private void dataGridViewX1_CellClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                hoaDonHienTai = dshoadon1[e.RowIndex];
            }
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            int maLophoc = lophoc.MA_LOP_HOC;
            frmLophoc frm =new frmLophoc(maLophoc);
            frm.Show();
        }
       
    }
}